import type { InfoReq, UserPageRes } from '@fast-crud/fast-crud';

import type { OnlineModel } from './model/onlineModel';

import type { PageParams, PageResult } from '#/api';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';
import { ContentTypeEnum } from '#/enums/httpEnum';

const MODULAR = 'defUser/onlineUsers';
const ServicePrefix = ServicePrefixEnum.SYSTEM;
export namespace OnlineApi {
  /**
   * 根据条件查询分页数据
   */
  export async function page(params: PageParams<any>) {
    return requestClient.post<PageResult<OnlineModel.OnlineUsersResultVO>>(
      `${ServicePrefix}/${MODULAR}/page`,
      params,
    );
  }

  export async function getTokenSignList(params: PageParams<any>) {
    return requestClient.post<OnlineModel.OnlineTokenResultVO[]>(
      `${ServicePrefix}/${MODULAR}/getTokenSignList`,
      params,
    );
  }

  export async function kickout(userId?: string, token?: string) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/kickout`,
      { userId, token },
      {
        headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
      },
    );
  }

  export async function logout(userId?: string, token?: string) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/logout`,
      { userId, token },
      {
        headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED },
      },
    );
  }

  export const pageRequest = async (
    pageQuery: PageParams<any>,
  ): Promise<UserPageRes> => {
    return await page(pageQuery);
  };

  export const getTokenSignListRequest = async (
    pageQuery: PageParams<any>,
  ): Promise<UserPageRes> => {
    return await getTokenSignList(pageQuery);
  };

  export const infoRequest = async (ctx: InfoReq): Promise<UserPageRes> => {
    const { row } = ctx;

    // 请求后台查询数据
    return row;
  };
}
